package com.lcq.gulimall.authsever.service.impl;

import com.lcq.common.exception.BizCodeEnum;
import com.lcq.common.utils.R;
import com.lcq.gulimall.authsever.constant.RegConstant;
import com.lcq.gulimall.authsever.feign.ThirdFeign;
import com.lcq.gulimall.authsever.service.RegService;

import com.lcq.gulimall.authsever.utils.AuthCodeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class RegServiceImpl implements RegService {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    ThirdFeign thirdFeign;
    @Override
    public R sendAuthCode(String phone) {
        String key = AuthCodeUtils.getSaveKey(phone);
        Long saveTime = AuthCodeUtils.getSaveTime(redisTemplate, phone);
        Long leftTime = System.currentTimeMillis()-saveTime;
        if (leftTime<60000){
            return R.error(BizCodeEnum.AUTH_REG_SMS_FREQUENT_EXCEPTION.getCode(),"操作过于频繁请稍后重试");
        }
        String code=AuthCodeUtils.createCode();
        AuthCodeUtils.saveCode(redisTemplate,phone,code);

        //thirdFeign.sendAuthCode(phone,code);
        return R.ok();
    }
}
